﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;

public static class MiaoDictionaryExtension {


    public static Tvalue TryGet<Tkey, Tvalue>(this Dictionary<Tkey, Tvalue> dict, Tkey key)
    {
        Tvalue value;
        dict.TryGetValue(key, out value);
        return value;
    }
    /// <summary>
    /// 重载整个字典对于Action添加方式。
    /// </summary>
    /// <typeparam name="Tkey"></typeparam>
    /// <param name="dict"></param>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public static void Add<Tkey>(this Dictionary<Tkey, Action> dict, Tkey key, Action value)
    {
        if (dict.ContainsKey(key))
        {
            dict[key] += value;
        }
        else
        {
            dict.Add(key, value);
        }
        return;
    }
    /// <summary>
    /// 重载整个字典对于Action添加方式。
    /// </summary>
    /// <typeparam name="Tkey"></typeparam>
    /// <param name="dict"></param>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public static void Add<Tkey>(this SortedDictionary<Tkey, Action> dict, Tkey key, Action value)
    {


        if (dict.ContainsKey(key))
        {
            dict[key] += value;
        }
        else
        {
            dict.Add(key, value);
        }
        return;
    }
    /// <summary>
    /// 重载对于特定Action的移除方式。
    /// </summary>
    /// <typeparam name="Tkey"></typeparam>
    /// <param name="dict"></param>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public static void Remove<Tkey>(this Dictionary<Tkey, Action> dict, Tkey key, Action value)
    {

        if (dict.ContainsKey(key))
        {
           Action AValue = dict[key];
           AValue -= value;


            if(AValue==null)//行
            {
                dict.Remove(key);
            }
        }
        return;
    }

    /// <summary>
    /// 对于委托组的添加方式。
    /// </summary>
    /// <typeparam name="Tkey"></typeparam>
    /// <param name="dict"></param>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public static void AddAction<Tkey>(this Dictionary<Tkey, Action[]> dict,Tkey key, Action actionGroup, int index,int length=3)
    {
        if (!dict.ContainsKey(key))
        {
            dict.Add(key, new Action[length]);
        }
        dict[key][index] += actionGroup;
        return;
    }
    /// <summary>
    /// 移除一个委托，但注意并不会判断委托组内委托是否都为空并且移出字典。
    /// </summary>
    /// <typeparam name="Tkey"></typeparam>
    /// <param name="dict"></param>
    /// <param name="key"></param>
    /// <param name="action"></param>
    /// <param name="index"></param>
    public static void RemoveAction<Tkey>(this Dictionary<Tkey, Action[]> dict, Tkey key, Action action, int index)
    {
        if (dict.ContainsKey(key))
        {
            dict[key][index] -= action;
        }
        return;
    }


    public static void Add<Tkey,T>(this Dictionary<Tkey, Action<T>> dict, Tkey key, Action<T> value)
    {
        if (dict.ContainsKey(key))
        {
            dict[key] += value;
        }
        else
        {
            dict.Add(key, value);
        }
        return;
    }

    public static void Remove<Tkey,T>(this Dictionary<Tkey, Action<T>> dict, Tkey key, Action<T> value)
    {

        if (dict.ContainsKey(key))
        {
            Action<T> AValue = dict[key];
            AValue -= value;


            if (AValue == null)//行
            {
                dict.Remove(key);
            }
        }
        return;
    }

    public static void Add<Tkey,T>(this Dictionary<Tkey, Tuple<Action,T>> dict, Tkey key, Tuple<Action, T> value)
    {
        if (dict.ContainsKey(key))
        {
           Action item = dict[key].Item1;
            item += value.Item1;
        }
        else
        {
            dict.Add(key, value);
        }
        return;
    }
    public static void RemoveTuple<Tkey,T>(this Dictionary<Tkey, Tuple<Action, T>> dict, Tkey key, Action ac)
    {

        if (dict.ContainsKey(key))
        {
            Action item = dict[key].Item1;
            item -= ac;


            if (item == null)
            {
                dict.Remove(key);
            }
        }
        return;
    }

}